
from fastapi import FastAPI, APIRouter, Request, Depends
from starlette import status
from starlette.middleware.cors import CORSMiddleware
from starlette.responses import JSONResponse
from controller.debugController import debug
from controller.webController import power, auth, task, case, document, application
from utils.permission import permission_verification
from utils.taskUtil import register_scheduler
from utils.tokenUtil import verify_token_format, analysis_token

app = FastAPI()

app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

permiss_app = APIRouter(dependencies=[Depends(permission_verification)])
permiss_app.include_router(power)
permiss_app.include_router(case)
permiss_app.include_router(task)
permiss_app.include_router(document)
permiss_app.include_router(application)
permiss_app.include_router(debug)

app.include_router(auth)
app.include_router(permiss_app)

register_scheduler(app)

@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    if request.url.path == "/auth/login" or request.url.path == "/docs" or request.url.path == "/openapi.json":
        return await call_next(request)

    tokenStr = request.headers.get("Authorization", None)
    tokenList = await verify_token_format(tokenStr)

    if type(tokenList) != list:
        return tokenList
    else:
        if await analysis_token(tokenList[1]):
            return await call_next(request)

    return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={"code": 604, "message": "token错误", "data": None})












































































